package com.dove.ohpm.ui

import com.dove.ohpm.service.Command
import com.dove.ohpm.service.ProjectLoader
import com.dove.ohpm.ui.custom.ButtonClickListener
import com.dove.ohpm.ui.custom.OperateType
import com.dove.ohpm.ui.custom.OperateType.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages

object KotlinListener : ButtonClickListener {
    override fun onButtonClick(type: OperateType, key: String, packageName: String, version: String) {
        try {
            when (type) {
                Install -> ProjectLoader.updateDependence(key, packageName, version)
                Delete -> ProjectLoader.removeDependence(key, packageName)
                Update -> ProjectLoader.updateDependence(key, packageName, version)
            }
            val path = ProjectLoader.applyChange(key)
            if (path != null)
                Command.execute("ohpm install", path)
        } catch (ex: Exception) {
            Messages.showErrorDialog(null as Project?, ex.message, "Error")
        }
    }
}